<?



require_once  $_SERVER['DOCUMENT_ROOT']."/../_lib/base/common.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.user_auth.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.template.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.newsletter.php";

	// See if user is logged in 
	$user_auth = new user_auth();
	$user_auth->user_auth_cp();

	$template = new template();







// FUNCTION // Display home ======================
function home(){
?>


<h2>Email Management</h2>



<h3>Bulk Insert</h3>

<?
if($GLOBALS['insert_note']){
	echo display_message($GLOBALS['insert_note']);
}
?>
<p>N.B. Files must be text files with one email per line.</p>

<form method='POST' action='<?= $_SERVER['PHP_SELF']?>' enctype="multipart/form-data">
	
	<input type="hidden" name="action" value="insert">

	<table>
	<tr>
	<td><label>File location:</label>&nbsp;&nbsp;</td>
	<td><input type="file" name="insert_file"  /></td>
	</tr>
	</table>

	<input name='Submit' value='Submit' type='submit'>
	
</form>

<br />
<br />


<h3>Bulk Removal</h3>


<?
if($GLOBALS['remove_note']){
	echo display_message($GLOBALS['remove_note']);
}
?>
<p>N.B. Files must be text files with one email per line.</p>

<form method='POST' action='<?= $_SERVER['PHP_SELF']?>' enctype="multipart/form-data">
	
	<input type="hidden" name="action" value="remove">

	<table>
	<tr>
	<td><label>File location:</label>&nbsp;&nbsp;</td>
	<td><input type="file" name="remove_file"    /></td>
	</tr>
	</table>

	<input name='Submit' value='Submit' type='submit'>
	
</form>

<br />
<br />


<h3>Email Download</h3>

<p><a href="<?= $_SERVER['PHP_SELF']?>?action=download">Download as text file</a></p>

<?
}







// Display confirm notice
function confirm_insert(){
?>

<h2>Email Management</h2>

<h3>Bulk Insert</h3>

<?
if($GLOBALS['insert_note']){
	echo display_message($GLOBALS['insert_note']);
}
?>

<form method='POST' action='<?= $_SERVER['PHP_SELF']?>'>
	
	<input type="hidden" name="action" value="commit_insert">
    
	<input value='Yes' type='submit' />
	<input value='No' type='submit' />
	
</form>



<?
}

// Display confirm notice
function confirm_remove(){
?>

<h2>Email Management</h2>

<h3>Bulk Remove</h3>

<?
if($GLOBALS['remove_note']){
	echo display_message($GLOBALS['remove_note']);
}
?>

<form method='POST' action='<?= $_SERVER['PHP_SELF']?>'>
	
	<input type="hidden" name="action" value="commit_remove">
    
	<input value='Yes' type='submit' />
	<input value='No' type='submit' />
	
</form>



<?
}



switch($_REQUEST['action']){
	default;
		$template->head_control();
		home();
		$template->foot_control();
	break;
	case "insert";
	// validate
	$template->head_control();
	$newsletter = new newsletter();
	$errors_found = $newsletter->validate_text_file_for_parsing($_FILES['insert_file']);
	$file_location = $_FILES['insert_file']['tmp_name'];
	
	if($errors_found){
		$my_message = array();
		$my_message['type'] = 'neg';
		$my_message['message'] = "I found the following problem(s) with your form:";
		$my_message['list'] = $errors_found;
		$GLOBALS['insert_note'] = $my_message;
		home();
	}else{
		$_SESSION['insert_emails'] = explode("\n",file_get_contents($file_location));
		$num_lines = count($_SESSION['insert_emails']);
		// confirm notice
		$my_message = array();
		$my_message['type'] = 'pos';
		$my_message['message'] = "Do you really want to upload ".$num_lines." emails?";
		$GLOBALS['insert_note'] = $my_message;
		confirm_insert();
	}
		$template->foot_control();
	break;
	case "remove";
	$template->head_control();
	// validate
	$newsletter = new newsletter();
	$errors_found = $newsletter->validate_text_file_for_parsing($_FILES['remove_file']);
	$file_location = $_FILES['remove_file']['tmp_name'];
	
	if($errors_found){
		$my_message = array();
		$my_message['type'] = 'neg';
		$my_message['message'] = "I found the following problem(s) with your form:";
		$my_message['list'] = $errors_found;
		$GLOBALS['remove_note'] = $my_message;
		home();
	}else{
		$_SESSION['remove_emails'] = explode("\n",file_get_contents($file_location));
		$num_lines = count($_SESSION['remove_emails']);
		// confirm notice
		$my_message = array();
		$my_message['type'] = 'pos';
		$my_message['message'] = "Do you really want to remove ".$num_lines." emails?";
		$GLOBALS['remove_note'] = $my_message;
		confirm_remove();
	}
		$template->foot_control();
	break;
	case "commit_insert";
		$newsletter = new newsletter();		
		$num_res = $newsletter->insert_bulk_email($_SESSION['insert_emails']);
		$my_message = array();
		$my_message['type'] = 'pos';
		$my_message['message'] = "There were ".$num_res." emails inserted";
		$GLOBALS['insert_note'] = $my_message;
		$template->head_control();
		home();
		$template->foot_control();
	break;
	case "commit_remove";
		$newsletter = new newsletter();		
		$num_res = $newsletter->remove_bulk_email($_SESSION['remove_emails']);
		$my_message = array();
		$my_message['type'] = 'pos';
		$my_message['message'] = "There were ".$num_res." emails removed";
		$GLOBALS['remove_note'] = $my_message;
		$template->head_control();
		home();
		$template->foot_control();
	break;
	case "download";
		
            /*
		header('Content-Type: plain/text; charset=utf-8');
		header('Content-Disposition: attachment; filename=latedeals_newsletter_emails.txt');
		
		$newsletter = new newsletter;
		$values = $newsletter->get_all();
		$lf = chr(10);
		$cr = chr(13);
		
		foreach($values as $value){
		
			$string .= $value['email'].$cr.$lf;
		
		}
			
		echo $string;
                
                */
		
                header("Content-type: text/csv");
                header("Content-Disposition: attachment; filename=file.csv");
                header("Pragma: no-cache");
                header("Expires: 0");

		$newsletter = new newsletter;
		$values = $newsletter->get_all();
		$lf = chr(10);
		$cr = chr(13);
		
		foreach($values as $value){
		
			$string .= $value['email'].$cr.$lf;
		
		}
			
		echo $string;
            
            
	break;
	
}

	

	
?>